package geektime.spring.springbuks.util;


import geektime.spring.springbuks.enums.CommonEnum;
import com.alibaba.fastjson.JSONObject;

import java.io.Serializable;

public class ResultBody<T> implements Serializable {
    /**
     * 鍝嶅簲浠ｇ爜
     */
    private String code;

    /**
     * 鍝嶅簲娑堟伅
     */
    private String message;

    /**
     * 鐩稿簲鏁版嵁
     */
    private Integer choice;
    /**
     * 鍝嶅簲缁撴灉
     */
    private Object result;

    public ResultBody() {
    }

    public ResultBody(BaseErrorInfoInterface errorInfo) {
        this.code = errorInfo.getResultCode();
        this.message = errorInfo.getResultMsg();
    }



    /**
     * 鍒嗛〉鏌ヨ
     * @param data
     * @return
     */
    public static ResultBody success(Object data,Integer choice) {
        ResultBody rb = new ResultBody();
        rb.setCode(CommonEnum.SUCCESS.getResultCode());
        rb.setMessage(CommonEnum.SUCCESS.getResultMsg());
        rb.setResult(data);
        rb.setChoice(choice);
        return rb;
    }



    public  ResultBody<T> successData(T value) {
        ResultBody<T> json = new ResultBody<>();
        json.setCode("200");
        return json;
    }

    /**
     * 澶辫触
     */
    public static ResultBody error(BaseErrorInfoInterface errorInfo) {
        ResultBody rb = new ResultBody();
        rb.setCode(errorInfo.getResultCode());
        rb.setMessage(errorInfo.getResultMsg());
        rb.setResult(null);
        return rb;
    }

    /**
     * 澶辫触
     */
    public static ResultBody error(String code, String message) {
        ResultBody rb = new ResultBody();
        rb.setCode(code);
        rb.setMessage(message);
        rb.setResult(null);
        return rb;
    }

    /**
     * 澶辫触
     */
    public static ResultBody error(String message) {
        ResultBody rb = new ResultBody();
        rb.setCode("403");
        rb.setMessage(message);
        rb.setResult(null);
        return rb;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getResult() {
        return result;
    }

    public void setResult(Object result) {
        this.result = result;
    }

    public Integer getChoice() {
        return choice;
    }

    public void setChoice(Integer choice) {
        this.choice = choice;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }


}
